package com.mqz.seckill.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQConfig {
    private static final String QUEUE = "queue";

    private static final String QUEUE_FANOUT01 = "queue_fanout01";
    private static final String QUEUE_FANOUT02 = "queue_fanout02";

    private static final String QUEUE_DIRECT01 = "queue_direct01";
    private static final String QUEUE_DIRECT02 = "queue_direct02";

    private static final String QUEUE_TOPIC01 = "queue_topic01";
    private static final String QUEUE_TOPIC02 = "queue_topic02";

    private static final String ROUTING_KEY01 = "channel01";
    private static final String ROUTING_KEY02 = "channel02";

    private static final String ROUTING_KEY03 = "#.queue.#";
    private static final String ROUTING_KEY04 = "*.queue.#";

    private static final String FANOUT_EXCHANGE = "fanout_exchange";
    private static final String DIRECT_EXCHANGE = "direct_exchange";
    private static final String TOPIC_EXCHANGE = "topic_exchange";

    @Bean
    public Queue queue() {
        return new Queue(QUEUE, true);
    }

    @Bean
    public Queue queue_fanout01() {
        return new Queue(QUEUE_FANOUT01, true);
    }

    @Bean
    public Queue queue_fanout02() {
        return new Queue(QUEUE_FANOUT02, true);
    }

    @Bean
    public Queue queue_direct01() {
        return new Queue(QUEUE_DIRECT01, true);
    }

    @Bean
    public Queue queue_direct02() {
        return new Queue(QUEUE_DIRECT02, true);
    }

    @Bean
    public Queue queue_topic01() {
        return new Queue(QUEUE_TOPIC01, true);
    }

    @Bean
    public Queue queue_topic02() {
        return new Queue(QUEUE_TOPIC02, true);
    }

    @Bean
    public FanoutExchange fanout_exchange() {
        return new FanoutExchange(FANOUT_EXCHANGE);
    }

    @Bean
    public DirectExchange direct_exchange() {
        return new DirectExchange(DIRECT_EXCHANGE);
    }

    @Bean
    public TopicExchange topic_exchange() {
        return new TopicExchange(TOPIC_EXCHANGE);
    }

    @Bean
    public Binding binding01() {
        return BindingBuilder.bind(queue_fanout01()).to(fanout_exchange());
    }

    @Bean
    public Binding binding02() {
        return BindingBuilder.bind(queue_fanout02()).to(fanout_exchange());
    }

    @Bean
    public Binding binding03() {
        return BindingBuilder.bind(queue_direct01()).to(direct_exchange()).with(ROUTING_KEY01);
    }

    @Bean
    public Binding binding04() {
        return BindingBuilder.bind(queue_direct02()).to(direct_exchange()).with(ROUTING_KEY02);
    }

    @Bean
    public Binding binding05() {
        return BindingBuilder.bind(queue_topic01()).to(topic_exchange()).with(ROUTING_KEY03);
    }

    @Bean
    public Binding binding06() {
        return BindingBuilder.bind(queue_topic02()).to(topic_exchange()).with(ROUTING_KEY04);
    }
}
